﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Juzzbott.Ulib
{
	public class UmbracoUtil
	{

		/// <summary>
		/// Gets the URL to a node from a content picker property.
		/// </summary>
		/// <param name="nodeID">The ID of the node that contains the content picker property.</param>
		/// <param name="propertyName">The name of the property to get the URL from.</param>
		/// <returns>The URL to the node from the content picker, or an empty string.</returns>
		public static string GetUrlFromNodeProperty(int nodeID, string propertyName)
		{

			// If the node ID is < 1, then its not valid, or if the propertyName is null or empty, then it's also invalid, so return empty string
			if (nodeID < 1 || String.IsNullOrEmpty(propertyName))
			{
				return "";
			}

			// Get the node from the ID
			UmbracoNode theNode = UmbracoNode.GetNode(nodeID);

			// If the node is null, then it doesn't exists, so it cant return the link, so return ""
			if (theNode == null)
			{
				return "";
			}

			// Get the value of the property
			int linkedNodeID = theNode.GetPropertyValue<int>(propertyName, 0);

			// If the linkedNodeID is 0, then it's invalid, so return ""
			if (linkedNodeID == 0)
			{
				return "";
			}

			// get the url to the linkedNodeID
			string nodeUrl = umbraco.library.NiceUrl(linkedNodeID);

			// return the nodeUrl
			return (!String.IsNullOrEmpty(nodeUrl) ? nodeUrl : "");

		}

	}
}
